<template>
  <div class="home-container">
    <!-- 顶部导航烂 -->
    <van-nav-bar class="page-nav-bar">
      <van-button
        class="search-btn"
        slot="title"
        type="info"
        size="small"
        round
        icon="search"
        to="/search"
        >搜索</van-button
      >
    </van-nav-bar>

    <!-- 频道列表 -->
    <van-tabs class="channel-tabs" v-model="active" swipeable animated>
      <van-tab
        :title="channel.name"
        v-for="channel in channels"
        :key="channel.id"
      >
        <!-- 文章列表 -->
        <article-list ref="article-list" :channel="channel" />
      </van-tab>

      <!-- 汉堡按钮 -->
      <div slot="nav-right" class="placeholder"></div>
      <div slot="nav-right" class="hamburger-btn">
        <i class="iconfont icon-gengduo" @click="isChannelEditShow = true"></i>
      </div>
    </van-tabs>
    <van-popup
      v-model="isChannelEditShow"
      closeable
      close-icon-position="top-left"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <channel-edit
        :my-channels="channels"
        :active-index="active"
        @update-active="onUpdateActive"
      />
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from "@/api/user";
import ArticleList from "./components/article-list";
import ChannelEdit from "./components/channel-edit.vue";
import { getItem } from "@/utlis/storage";
export default {
  data() {
    return {
      active: 0, //默认列表
      channels: [], //储存容器
      isChannelEditShow: false, // 控制编辑频道弹出层的显示状态
    };
  },
  components: {
    ArticleList,
    ChannelEdit,
  },
  // 其他节点略
  created() {
    this.loadChannels();
  },
  name: "HomeIndex",
  methods: {
    // 请求数据
    // 我的频道高亮和弹窗跳转
    onUpdateActive(index, isChannelEditShow) {
      this.active = index; // 切换我的频道高亮
      this.isChannelEditShow = isChannelEditShow; // 通过隐藏弹窗，active切换到响应的频道项
    },
    async loadChannels() {
      try {
        if (this.user) {
          // 登录？
          const {
            data: {
              data: { channels },
            },
          } = await getUserChannels();
          this.channels = channels;
        } else {
          // 未登录
          const locationChannels = getItem("TOUTIAO_CHANNELS");
          if (locationChannels) {
            // 本地有数据？
            this.channels = locationChannels;
          } else {
            // 没有数据？
            const {
              data: {
                data: { channels },
              },
            } = await getUserChannels();
            this.channels = channels;
          }
        }
      } catch (error) {
        this.$toast("获取频道数据失败");
      }
    },
    // 删除切换导航
    // onApdataActive (index, isChannelEditShow = true) {
    //   // this.channels.splice(index, 1)
    //   this.active = index
    //   this.isChannelEditShow = isChannelEditShow
    // }
  },
};
</script>
<style scoped lang="less">
.home-container {
  /deep/.van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
  .placeholder {
    flex-shrink: 0;
    width: 66px;
    height: 82px;
  }

  .hamburger-btn {
    position: fixed;
    right: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 66px;
    height: 82px;
    background-color: #fff;
    opacity: 0.902;

    i.toutiao {
      font-size: 33px;
    }

    &:before {
      content: "";
      position: absolute;
      left: 0;
      width: 1px;
      height: 100%;
      background-image: url(~@/assets/gradient-gray-line.png);
      background-size: contain;
    }
  }
}
</style>